import { fakeparticleSystemEffectClickCallback } from '@/service';
import { generateListLayerName } from '@/service/layer';
import { useSceneStore } from '@/store/scene';
import { retFilePath } from '@/utils';
import { nextTick } from 'vue';

export const addFakeParticleSystemEffect = (selEffect: any) => {
  const sceneStore = useSceneStore();

  // 参数化特效
  function addFakeParticleSystem(position: number[]) {
    const { cimInstance } = window;
    if (!selEffect.value) return;
    const effectTemporaryAttr: any = sceneStore.effectTemporaryAttr;
    const layerName = '参数化特效' + Math.random().toString().split('.')[1];
    const modelJson = {
      name: layerName,
      type: 'entity',
      views: [
        {
          enable: true,
          name: layerName,
          type: 'gltfmodel',
          createType: 'primitive',
          positions: [position],
          rotation: [0, 0, 0],
          dataSourceUrl: retFilePath('comm/effects/defParticle/empty.gltf'),
          scale: 1,
          enablePbr: true,
          shadowsType: 1,
          iblConfig: {
            environmentMaps: '/cimupresource/image/environment/noon.ktx2',
          },
          show: true,
        },
      ],
      components: [
        {
          enable: true,
          name: Math.random().toString().split('.')[1],
          type: 'fake_particleSystem',
          emitterStr: 'CircleWave',
          speed: effectTemporaryAttr ? effectTemporaryAttr.speed : 10,
          semiMinorAxis: effectTemporaryAttr ? effectTemporaryAttr.semiMinorAxis : 300,
          semiMajorAxis: effectTemporaryAttr ? effectTemporaryAttr.semiMajorAxis : 300,
          circleCount: effectTemporaryAttr ? effectTemporaryAttr.circleCount : 3,
          color: '#ffffff',
          offset: [0, 0, 0],
          vName: '粒子2',
          longitude: position[0],
          latitude: position[1],
          height: position[2],
          owner: layerName,
          show: true,
        },
      ],
    };
    const layerParam: API.Scene.ILayer = {
      code: Math.random().toString().split('.')[1],
      show: true,
      json: modelJson,
      name: generateListLayerName('fake_particleSystem'),
      layerName: modelJson.name,
      desc: '',
      type: 'fake_particleSystem',
    };
    cimInstance.api.closeSceneOffset();
    sceneStore.layerList.unshift(layerParam);
    nextTick(() => {
      fakeparticleSystemEffectClickCallback({
        code: 'effect',
        clickObject: {
          id: layerParam.layerName,
        },
      });
    });
  }

  return {
    addFakeParticleSystem,
  };
};
